﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
public partial class hienthongtindn : System.Web.UI.Page
{
    DatabaseHandler dbHandler = new DatabaseHandler();
    string query="SELECT * FROM tbl_doanhnghiep";

    void loadComboBox(DropDownList ddl)
    {
        ddl.Items.Clear();
        ddl.Items.Add("Tất cả các doanh nghiệp");
        string sql = "SELECT * FROM tbl_nganh";
        addCombobox.addcb(ddl, sql, "TenNganh");
        
     
    }


    void loadTableHeaderForViewer()
    {
        TableHeaderRow headerRow = new TableHeaderRow();
        headerRow.BackColor = Color.LightBlue;

        // Create TableCell objects to contain 
        // the text for the header.
        TableHeaderCell headerTableCell1 = new TableHeaderCell();
        TableHeaderCell headerTableCell2 = new TableHeaderCell();
        TableHeaderCell headerTableCell3 = new TableHeaderCell();
        TableHeaderCell headerTableCell4 = new TableHeaderCell();
        TableHeaderCell headerTableCell5 = new TableHeaderCell();
        headerTableCell1.Text = "STT";
        headerTableCell1.Scope = TableHeaderScope.Column;
        headerTableCell1.AbbreviatedText = "Col 1 Head";
        
        headerTableCell2.Text = "Tên Doanh Nghiệp";
        headerTableCell2.Scope = TableHeaderScope.Column;
        headerTableCell2.AbbreviatedText = "Col 2 Head";
        
        headerTableCell3.Text = "Địa Chỉ";
        headerTableCell3.Scope = TableHeaderScope.Column;
        headerTableCell3.AbbreviatedText = "Col 3 Head";
        
        headerTableCell4.Text = "Số Điện Thoại";
        headerTableCell4.Scope = TableHeaderScope.Column;
        headerTableCell4.AbbreviatedText = "Col 4 Head";
        
        headerTableCell5.Text = "Email";
        headerTableCell5.Scope = TableHeaderScope.Column;
        headerTableCell5.AbbreviatedText = "Col 4 Head";

        // Add the TableHeaderCell objects to the Cells
        // collection of the TableHeaderRow.
        headerRow.Cells.Add(headerTableCell1);
        headerRow.Cells.Add(headerTableCell2);
        headerRow.Cells.Add(headerTableCell3);
        headerRow.Cells.Add(headerTableCell4);
        headerRow.Cells.Add(headerTableCell5);
        myTable.Rows.AddAt(0, headerRow);
    }
    
    void loadTableHeaderForEditor()
    {
        TableHeaderRow headerRow = new TableHeaderRow();
        headerRow.BackColor = Color.LightBlue;

        // Create TableCell objects to contain 
        // the text for the header.
        TableHeaderCell headerTableCell1 = new TableHeaderCell();
        TableHeaderCell headerTableCell2 = new TableHeaderCell();
        TableHeaderCell headerTableCell3 = new TableHeaderCell();
        headerTableCell1.Text = "STT";
        headerTableCell1.Scope = TableHeaderScope.Column;
        headerTableCell1.AbbreviatedText = "Col 1 Head";
        headerTableCell2.Text = "Tên Doanh Nghiệp";
        headerTableCell2.Scope = TableHeaderScope.Column;
        headerTableCell2.AbbreviatedText = "Col 2 Head";
        headerTableCell3.Text = "Địa Chỉ";
        headerTableCell3.Scope = TableHeaderScope.Column;
        headerTableCell3.AbbreviatedText = "Col 3 Head";
       

       
        headerRow.Cells.Add(headerTableCell1);
        headerRow.Cells.Add(headerTableCell2);
        headerRow.Cells.Add(headerTableCell3);
        myTable.Rows.AddAt(0, headerRow);
    }


    LinkButton lbtnTenDoanhNghiep;


    void loadTable()
    {
        string IDDN;
        TableRow tableRow;
        Label lbSTT, lbDiaChi, lbDienThoai, lbMail;
        MySqlDataReader reader;

        dbHandler.connect();
        reader = dbHandler.dataReader(query);
        int stt = 0;

        //true is Editor
        if (Variables.roleFlag)
        {

            loadTableHeaderForEditor();

            while (reader.Read())
            {
                stt += 1;
                tableRow = new TableRow();

                lbSTT = new Label();
                lbtnTenDoanhNghiep = new LinkButton();
                lbDiaChi = new Label();



                TableCell cell0 = new TableCell();
                lbSTT.Text = stt.ToString();
                cell0.Controls.Add(lbSTT);
                tableRow.Cells.Add(cell0);

                IDDN = reader.GetString("IDDN");


                TableCell cell2 = new TableCell();
                lbtnTenDoanhNghiep.ID = IDDN;
                lbtnTenDoanhNghiep.Click += new EventHandler(lbtnTenDoanhNghiep_Click);
                lbtnTenDoanhNghiep.Text = reader.GetString("TENDN");
                cell2.Controls.Add(lbtnTenDoanhNghiep);
                tableRow.Cells.Add(cell2);

                TableCell cell3 = new TableCell();
                lbDiaChi.Text = reader.GetString("DIACHI");
                cell3.Controls.Add(lbDiaChi);
                tableRow.Cells.Add(cell3);



                myTable.Rows.Add(tableRow);
            }
        }
        else // for Viewer
        {
            loadTableHeaderForViewer();
            while (reader.Read())
            {
                stt += 1;
                tableRow = new TableRow();

                lbSTT = new Label();
                lbtnTenDoanhNghiep = new LinkButton();
                lbDiaChi = new Label();
                lbDienThoai = new Label();
                lbMail = new Label();


                TableCell cell0 = new TableCell();
                lbSTT.Text = stt.ToString();
                cell0.Controls.Add(lbSTT);
                tableRow.Cells.Add(cell0);

                IDDN = reader.GetString("IDDN");


                TableCell cell2 = new TableCell();
                lbtnTenDoanhNghiep.ID = IDDN;
                lbtnTenDoanhNghiep.Text = reader.GetString("TENDN");
                cell2.Controls.Add(lbtnTenDoanhNghiep);
                tableRow.Cells.Add(cell2);

                TableCell cell3 = new TableCell();
                lbDiaChi.Text = reader.GetString("DIACHI");
                cell3.Controls.Add(lbDiaChi);
                tableRow.Cells.Add(cell3);

                TableCell cell4 = new TableCell();
                lbDienThoai.Text = reader.GetString("DIENTHOAI");
                cell4.Controls.Add(lbDienThoai);
                tableRow.Cells.Add(cell4);

                TableCell cell5 = new TableCell();
                lbMail.Text = reader.GetString("EMAIL");
                cell5.Controls.Add(lbMail);
                tableRow.Cells.Add(cell5);


                myTable.Rows.Add(tableRow);
            }
        }

        dbHandler.close();
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        loadComboBox(ddlSelect);
        loadTable();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
      
       
    }



    void lbtnTenDoanhNghiep_Click(Object sender, EventArgs e)
    {

        var yourControl = (LinkButton)sender;
        Variables.IDDN = yourControl.ID.ToString();

        Response.Redirect("CapNhat_TTDoanhNghiep.aspx");
    }
    protected void ThemMoi_Click(object sender, EventArgs e)
    {
        Response.Redirect("Nhap_TTDoanhNghiep.aspx");
    }
    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        myTable.Rows.Clear();
        string sqlAll = "SELECT * FROM tbl_doanhnghiep";
        string sqlSelect = "SELECT * FROM tbl_doanhnghiep WHERE NHOMNGANH='" + ddlSelect.SelectedValue + "'";

        if (ddlSelect.SelectedIndex == 0)
            query = sqlAll;
        else
            query = sqlSelect;

        loadTable();
    }
}